home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 12861 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.7 KB  |  70 lines

  1. Path: ssds.com!usenet
  2. From: Ron Romero <ron.romero@ssds.com>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: file processing...
  5. Date: Thu, 21 Mar 1996 13:37:30 -0500
  6. Organization: SSDS, Inc.
  7. Distribution: inet
  8. Message-ID: <3151A1EA.647C@ssds.com>
  9. References: <4iqr98$i3p@netnews.upenn.edu>
  10. NNTP-Posting-Host: rem_vie26.ssds.com
  11. Mime-Version: 1.0
  12. Content-Type: text/plain; charset=us-ascii
  13. Content-Transfer-Encoding: 7bit
  14. X-Mailer: Mozilla 2.0 (Win16; I)
  15. CC: ron.romero@ssds.com
  16.  
  17. Sonny wrote:
  18. > I want to read the 1st line of a text file into an array. The only way I know of to
  19. > detect the end of a line is by checking for "\n", but the code I wrote does not seem
  20. > be detecting it. Here is the code...
  21. > #include<fstream.h>
  22. > #include<iostream.h>
  23. > #include<String.h>
  24. > int readf(char array[],char *);
  25. > int readf (char array[], char *filename)
  26. > {
  27. >   int i=0;
  28. >   char c;
  29. >   ifstream inFile(filename, ios::in);
  30. >   while (inFile >> c)
  31. >     if (c!="\n")      /*This line is giving me problem. It doesn't even compile */
  32. >       array[i++]=c;
  33. >     else
  34. >       break;
  35. >   return i;
  36. > }
  37. > [main deleted]
  38.  
  39. The short answer is that you're comparing a character to a string, so you're 
  40. getting a type mismatch error.  Double quotes mark a string; single quotes mark 
  41. a character.  So the line that's giving you trouble should be 
  42.   if (c!='\n')
  43.  
  44. The long answer is that you should be using istream::getline.  This would make 
  45. your main look like:
  46.  
  47. void main(int argc,char *argv[])
  48. {
  49.   int i;
  50.   char array[50];
  51.   ifstream inFile("gro", ios::in);
  52.   inFile.getline(array, 50, '\n');
  53.  
  54.   for(int c=0; array[c] != '\0'; c++)
  55.     cout<< array[c];
  56.   cout << "\n";
  57. }
  58.  
  59. Hope this helps.
  60.  
  61. --------------------
  62. Ron Romero
  63. ron.romero@ssds.com
  64.